(function() {
    'use strict';
    /**
     * @ngdoc function
     * @name webApp.controller:LabelCtrl
     * @description
     * # LabelCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('LabelCtrl', LabelCtrl);

    LabelCtrl.$inject = ['$scope', 'dataService', '$location', '$state', 'helper', '$timeout', '$rootScope', 'sku', 'MENU_Data', 'cacheService'];

    function LabelCtrl($scope, dataService, $location, $state, helper, $timeout, $rootScope, sku, MENU_Data, cacheService) {
        //商品显示
        $scope.listAction = function() {
            $scope.getLabelList();
        }

        //获取列表
        $scope.getLabelList = function(pageNo) {
            var query = {};
            var key = $rootScope.store_id + "LabelCtrl";
            var cache = cacheService.get(key);
            if (pageNo || !cache) {
                $scope.pageNo = pageNo;
                query = {
                    page_no: $scope.pageNo,
                    page_size: $scope.pageSize,
                    store_id: $rootScope.store_id,
                };

                if ($scope.keyword) {
                    query.keyword = $scope.keyword;
                }
                cacheService.set(key, query);
            } else {
                query = clone(cache);
                $scope.queryRcache(query);
            }
            var querys = clone(query);

            dataService.labelGet(querys).then(function(list) {
                if (list) {
                    $scope.label_list = list.label_list;
                    $scope.itemsTotal = list.total_count;
                }
            });
        }

        $scope.queryRcache = function(query) {
            $scope.pageNo = query.page_no;
            $scope.page_size = query.page_size;
            if (query.keyword) {
                $scope.keyword = query.keyword;
            }
        };

        //添加标签
        $scope.labelAdd = function() {
                if ($scope.label) {
                    $scope.label.store_id = $rootScope.store_id;
                    console.error("label", $scope.label);
                    dataService.labelAdd($scope.label).then(function(list) {
                        if (list) {
                            helper.myAlert("保存成功！", "#/label/list");
                        }
                    });
                }
            }
            //删除
        $scope.del = function(id) {
                if (id) {
                    var query = {
                        "label_id": id
                    };
                    dataService.labelDel(query).then(function(list) {
                        if (list) {
                            helper.myAlert("成功！", "#/label/list");
                            $scope.getLabelList();
                        }
                    });
                }
            }
            //添加修改操作
        $scope.addAction = function() {
            $scope.label_id = $location.search().id;
            if ($scope.label_id) {
                var query = {
                    label_id: $scope.label_id,
                    store_id: $rootScope.store_id,
                };
                dataService.labelGet(query).then(function(list) {
                    if (list) {
                        $scope.label = list.label_list[0];
                    }
                });
            }
        }
    }
})();